<template>
  <el-dialog
    v-el-drag-dialog
    title="选择客户"
    :visible.sync="visible"
    width="1000px"
    @closed="reset"
  >
    <div>
      <div class="top-name">
        <span class="label">优惠权益</span>
        <el-select v-model="preferentialPackageId">
          <el-option
            v-for="item in preferentialList"
            :key="item.preferentialPackageId"
            :label="item.preferentialPackageName"
            :value="item.preferentialPackageId"
          />
        </el-select>
      </div>
      <div>
        <el-table-form ref="form" :model="query">
          <el-form-item class="merge-item">
            <el-select v-model="qtype" style="width: 170px; margin-right: 2px;">
              <el-option value="csmRgPhone" label="客户电话" />
              <el-option value="csmCode" label="客户编号" />
              <el-option value="csmName" label="客户名称" />
              <el-option value="csmRgAddress" label="客户地址" />
              <el-option value="sfzh" label="身份证号" />
            </el-select>
            <el-input v-model.trim="txt" placeholder="请输入" clearable />
          </el-form-item>
          <el-form-item label="所属机构:" prop="orgSNNo">
            <el-tree-select
              v-model="query.orgSNNo"
              :clearable="true"
              class="tree-select"
              :popover-width="360"
              :select-params="{
                clearable: true,
                placeholder: '请选择',
              }"
              :tree-params=" {
                'expand-on-click-node': false,
                'highlight-current': true,
                'filterable': true,
                props:{
                  value:'orgSNNo',
                  label: 'orgName',
                  disabled: 'disabled',
                },
                data: treeData
              }"
            />
          </el-form-item>
          <template v-slot:btns>
            <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
            <el-button type="primary" style="margin-right: 5px;" icon="el-icon-plus" @click="handleAdd">新增</el-button>
          </template>
        </el-table-form>
        <lb-table
          v-if="visible"
          ref="lbTable"
          border
          highlight-current-row
          element-loading-text="加载中"
          :column="tableData.column"
          :data="tableData.data"
          pagination
          custom-column
          max-height="300"
          @fetch="fetchData"
          @selection-change="handleSelectionChange"
        />
        <span class="total">共{{ tableData.total }}条记录</span>
      </div>
    </div>
  </el-dialog>
</template>

<script>
  import { preferentialSelect, customerQuery, customerPreferentialAdd } from '@/api'
  import getTreeData from '@/utils/jsonTree'
  import { formatColumnMap } from '@/utils'
  import { mapGetters } from 'vuex'
  export default {
    name: 'addClient',
    data() {
      return {
        visible: false,
        preferentialPackageId: '',
        preferentialList: [],
        qtype: 'csmRgPhone',
        txt: '',
        query: {
          orgSNNo: ''
        },
        treeData: [],
        tableData: {
          column: [
            {
              type: 'selection'
            },
            {
              prop: 'csmCode',
              label: '客户编号'
            },
            {
              prop: 'csmName',
              label: '客户名称'
            },
            {
              prop: 'csmRgPhone',
              width: 100,
              label: '电话'
            },
            {
              prop: 'csmAddress',
              width: 150,
              label: '地址'
            },
            {
              prop: 'sfzh',
              width: 120,
              label: '身份证'
            },
            {
              prop: 'orgName',
              width: 120,
              label: '所属机构'
            }
          ],
          data: [],
          total: 0
        },
        multipleSelection: []
      }
    },
    computed: {
      ...mapGetters([
        'orgList'
      ])
    },
    async created() {
    },
    mounted() {

    },
    methods: {
      async open() {
        this.visible = true
        this.preferentialPackageId = ''
        this.preferentialList = []
        this.multipleSelection = []
        this.qtype = 'csmRgPhone'
        this.txt = ''
        this.query.orgSNNo = ''
        this.getTreeData()
        this.preferentialSelect()
      },
      // 获取所属机构数据
      async getTreeData() {
        const data = this.orgList.map(item => ({
          label: item.orgName,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      // 获取权益列表
      async preferentialSelect() {
        const params = {
          currentPage: 1,
          pageSize: 9999
        }
        const res = await preferentialSelect(params)
        this.preferentialList = res.data.rows
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          orgSNNo: this.query.orgSNNo,
          currentPage,
          pageSize
        }
        query[this.qtype] = this.txt
        const res = await customerQuery(query)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      handleAdd() {
        if (!this.preferentialPackageId) {
          this.$message.warning('请选择优惠权益')
          return
        }
        if (this.multipleSelection.length === 0) {
          this.$message.warning('请选择客户')
          return
        }
        const paramsArr = []
        this.multipleSelection.forEach((item) => {
          const obj = {
            csmAddressId: item.csmAddressId,
            preferentialPackageId: this.preferentialPackageId,
            preferentialPackageName: formatColumnMap(this.preferentialList, 'preferentialPackageId', this.preferentialPackageId, 'preferentialPackageName')
          }
          paramsArr.push(obj)
        })
        console.log(paramsArr)
        customerPreferentialAdd(paramsArr).then(res => {
          if (res.data) {
            this.visible = false
            this.$message.success('新增成功')
            this.$emit('refresh')
          }
        })
      },
      reset() {

      }
    }
  }
</script>

<style lang="scss" scoped>
  .top-name {
    display: flex;
    margin-bottom: 10px;
    .label {
      width: 90px;
      line-height: 30px;
    }
  }
  .total {
    position: absolute;
    right: 40px;
    bottom: 25px;
  }
</style>
